package com.ardf.controller;

import com.ardf.common.CodeEnum;
import com.ardf.common.ReturnResult;
import com.ardf.entity.Number;
import com.ardf.entity.dto.NumberSearchAllDto;
import com.ardf.service.INumberService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ShiYunHao
 * @since 2025-01-06
 */
@RestController
@RequestMapping("/ardf/number")
public class NumberController {
    private final INumberService numberService;
    public NumberController(INumberService numberService){
        this.numberService=numberService;
    }

    /**
     *分页
     */
    @GetMapping("/searchAll")
    public ResponseEntity<Page<NumberSearchAllDto>> searchAll(@RequestParam("pageSize") Integer pageSize,
                                                              @RequestParam("pageNum") Integer pageNum,
                                                              @RequestParam("competitionId") Long competitionId,
                                                              @RequestParam(value = "searchAll", required = false) String searchAll) {
        Page<NumberSearchAllDto> page;
        if (StringUtils.hasLength(searchAll.trim())) {
            page = numberService.searchAll(pageSize, pageNum, competitionId, searchAll.trim());
        } else {
            page = numberService.searchAll(pageSize, pageNum, competitionId, null);
        }
        return ResponseEntity.ok(page);
    }

    @GetMapping("/containEmpty")
    public ResponseEntity<ReturnResult> containEmpty(@RequestParam("competitionId") Long competitionId) {
        Boolean b = numberService.containsEmpty(competitionId);
        ReturnResult result=new ReturnResult(CodeEnum.SUCCESS);
        result.setData(b);

        return ResponseEntity.ok(result);
    }

    /**
     * 一键下发
     */
    @PostMapping("/oneClick")
    public void oneClick( @RequestParam("competitionId") Long competitionId){
        numberService.oneClick(competitionId);
    }
    /**
     * 查重
     */
    @GetMapping("/check")
    public ResponseEntity<String> check( @RequestParam("competitionId") Long competitionId,
                                         @RequestParam("number") String number,
                                         @RequestParam("numberId") Long numberId){
        if(numberService.check(competitionId,number,numberId)){
            return ResponseEntity.ok("yes");
        }else {
            return ResponseEntity.ok("no");
        }
    }

    /**
     * 更新选手号码
     */
    @PostMapping("/update")
    public void update(@RequestBody Number number){
        numberService.updateById(number);
    }

    /**
     * 下载选手号码表
     */
    @GetMapping("/download")
    public void download(HttpServletResponse response,@RequestParam("competitionId") Long competitionId) throws IOException {
        numberService.download(response,competitionId);
    }
}
